﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MethodWorx.CMS.UI.Application;
using MethodWorx.CMS.UI.Areas.Application.Models;
using MethodWorx.CMS.Plugins.ActionResults;
using MethodWorx.CMS.UI.Application.ActionResults;
using MethodWorx.Core.DomainModel;
using MethodWorx.Core.Validation;

namespace MethodWorx.CMS.UI.Areas.Application.Controllers
{
    public class CreateContentItemController : ApplicationController, IValidationProvider
    {
        private IModelFactory<CreateContentItemModel> modelFactory;

        public CreateContentItemController(IModelFactory<CreateContentItemModel> modelFactory)
        {
            this.modelFactory = modelFactory;
        }

        [HttpGet]
        public ActionResult ChooseContentTemplate(Guid id)
        {
            var model = this.modelFactory.CreateModel(this.CurrentUser);
            model.Initialize(id);

            return View("ChooseContentTemplate", model);
        }

        [HttpPost]
        public ActionResult ChooseContentTemplate(Guid id, FormCollection form)
        {
            var model = this.modelFactory.CreateModel(this.CurrentUser);
            model.Initialize(id);
            this.TryUpdateModel(model.Request, "Request", form);

            if (model.Request.ExistingContentTemplate == null && string.IsNullOrEmpty(model.Request.NewTemplateName))
                this.ModelState.AddModelError("Request.NewTemplateName", "This field is required");

            //  do this validation to ensure we can cretae another page here 
            //  of the given template
            model.ValidatePublishedContentTemplates(model.Request, this);

            if (this.ModelState.IsValid)
            {
                return View("CreateContent", model);
            }
            else
                return View("ChooseContentTemplate", model);
        }



        [HttpPost]
        public ActionResult CreateContent(Guid id, FormCollection form)
        {
            var model = this.modelFactory.CreateModel(this.CurrentUser);
            model.Initialize(id); ;
            this.TryUpdateModel(model.Request, "Request", form);

            if (model.Request.ExistingContentTemplate == null && string.IsNullOrEmpty((model.Request.NewTemplateName ?? "").Trim()))
                this.ModelState.AddModelError("Request.NewTemplateName", "This field is required");

            if (string.IsNullOrEmpty((model.Request.ContentName ?? "").Trim()))
                this.ModelState.AddModelError("Request.PageName", "This field is required");

            if (this.ModelState.IsValid)
            {
                try
                {
                    model.Complete();
                    return new RefreshTreeNodeResult().And(new CloseDialogResult("ok"));
                }
                catch (BusinessRuleException bre)
                {
                    bre.AddToModelState(this.ModelState);
                }
            }

            return View("CreateContent", model);
        }


        public bool Validate(object instance)
        {
            return true;
        }

        public void AddError(string key, string message)
        {
            this.ModelState.AddModelError("Request." + key, message);
        }

        public bool IsValid()
        {
            return this.ModelState.IsValid;
        }
    }
}
